home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / direct3d / envmapping / spheremap / spheremap.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  11.9 KB  |  347 lines

  1. //-----------------------------------------------------------------------------
  2. // File: SphereMap.cpp
  3. //
  4. // Desc: Example code showing how to use sphere-mapping in D3D, using generated 
  5. //       texture coordinates.
  6. //
  7. // Copyright (c) 1997-2000 Microsoft Corporation. All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #define STRICT
  10. #include <tchar.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <D3DX8.h>
  14. #include "D3DApp.h"
  15. #include "D3DFile.h"
  16. #include "D3DFont.h"
  17. #include "D3DUtil.h"
  18. #include "DXUtil.h"
  19.  
  20.  
  21.  
  22.  
  23. //-----------------------------------------------------------------------------
  24. // Name: class CMyD3DApplication
  25. // Desc: Application class. The base class (CD3DApplication) provides the 
  26. //       generic functionality needed in all Direct3D samples. CMyD3DApplication 
  27. //       adds functionality specific to this sample program.
  28. //-----------------------------------------------------------------------------
  29. class CMyD3DApplication : public CD3DApplication
  30. {
  31.     CD3DFont*          m_pFont;
  32.     CD3DArcBall        m_ArcBall;
  33.     CD3DMesh*          m_pTeapot;
  34.     LPDIRECT3DTEXTURE8 m_pSphereMapTexture;
  35.     BOOL               m_bGlassEffect;
  36.  
  37. protected:
  38.     HRESULT OneTimeSceneInit();
  39.     HRESULT InitDeviceObjects();
  40.     HRESULT RestoreDeviceObjects();
  41.     HRESULT InvalidateDeviceObjects();
  42.     HRESULT DeleteDeviceObjects();
  43.     HRESULT Render();
  44.     HRESULT FrameMove();
  45.     HRESULT FinalCleanup();
  46.  
  47. public:
  48.     LRESULT MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
  49.  
  50.     CMyD3DApplication();
  51. };
  52.  
  53.  
  54.  
  55.  
  56. //-----------------------------------------------------------------------------
  57. // Name: WinMain()
  58. // Desc: Entry point to the program. Initializes everything, and goes into a
  59. //       message-processing loop. Idle time is used to render the scene.
  60. //-----------------------------------------------------------------------------
  61. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  62. {
  63.     CMyD3DApplication d3dApp;
  64.  
  65.     if( FAILED( d3dApp.Create( hInst ) ) )
  66.         return 0;
  67.  
  68.     return d3dApp.Run();
  69. }
  70.  
  71.  
  72.  
  73.  
  74. //-----------------------------------------------------------------------------
  75. // Name: CMyD3DApplication()
  76. // Desc: Application constructor. Sets attributes for the app.
  77. //-----------------------------------------------------------------------------
  78. CMyD3DApplication::CMyD3DApplication()
  79. {
  80.     m_strWindowTitle    = _T("SphereMap: Environment Mapping Technique");
  81.     m_bUseDepthBuffer   = TRUE;
  82.     m_bShowCursorWhenFullscreen = TRUE;
  83.  
  84.     m_pFont             = new CD3DFont( _T("Arial"), 12, D3DFONT_BOLD );
  85.     m_pTeapot           = new CD3DMesh();
  86.     m_pSphereMapTexture = NULL;
  87.     m_bGlassEffect      = FALSE;
  88. }
  89.  
  90.  
  91.  
  92.  
  93. //-----------------------------------------------------------------------------
  94. // Name: OneTimeSceneInit()
  95. // Desc: Called during initial app startup, this function performs all the
  96. //       permanent initialization.
  97. //-----------------------------------------------------------------------------
  98. HRESULT CMyD3DApplication::OneTimeSceneInit()
  99. {
  100.     // Set cursor to indicate that user can move the object with the mouse
  101. #ifdef _WIN64
  102.     SetClassLongPtr( m_hWnd, GCLP_HCURSOR, (LONG_PTR)LoadCursor( NULL, IDC_SIZEALL ) );
  103. #else
  104.     SetClassLong( m_hWnd, GCL_HCURSOR, (LONG)LoadCursor( NULL, IDC_SIZEALL ) );
  105. #endif
  106.     return S_OK;
  107. }
  108.  
  109.  
  110.  
  111.  
  112. //-----------------------------------------------------------------------------
  113. // Name: FrameMove()
  114. // Desc: Called once per frame, the call is the entry point for animating
  115. //       the scene.
  116. //-----------------------------------------------------------------------------
  117. HRESULT CMyD3DApplication::FrameMove()
  118. {
  119.     // Update the object's rotation angle
  120.     static FLOAT fRotationAngle = 0.0f;
  121.     if( FALSE == m_ArcBall.IsBeingDragged() )
  122.         fRotationAngle += m_fElapsedTime;
  123.  
  124.     // Setup viewing postion from ArcBall
  125.     D3DXMATRIX matWorld;
  126.     D3DXMATRIX matRotationInverse;
  127.     D3DXMatrixRotationY( &matWorld, -fRotationAngle );
  128.     D3DXMatrixInverse( &matRotationInverse, NULL, m_ArcBall.GetRotationMatrix() );
  129.     D3DXMatrixMultiply( &matWorld, &matWorld, &matRotationInverse );
  130.     D3DXMatrixMultiply( &matWorld, &matWorld, m_ArcBall.GetTranslationMatrix() );
  131.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  132.  
  133.     return S_OK;
  134. }
  135.  
  136.  
  137.  
  138.  
  139. //-----------------------------------------------------------------------------
  140. // Name: Render()
  141. // Desc: Called once per frame, the call is the entry point for 3d
  142. //       rendering. This function sets up render states, clears the
  143. //       viewport, and renders the scene.
  144. //-----------------------------------------------------------------------------
  145. HRESULT CMyD3DApplication::Render()
  146. {
  147.     // Clear the viewport
  148.     m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  149.                          0x000000ff, 1.0f, 0L );
  150.  
  151.     // Begin the scene 
  152.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  153.     {
  154.         // Adds in the spheremap texture
  155.         m_pd3dDevice->SetTexture( 0, m_pSphereMapTexture );
  156.         m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  157.         m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  158.         m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  159.  
  160.         // Generate spheremap texture coords, and shift them over
  161.         D3DXMATRIX mat;
  162.         mat._11 = 0.5f; mat._12 = 0.0f; mat._13 = 0.0f; mat._14 = 0.0f; 
  163.         mat._21 = 0.0f; mat._22 =-0.5f; mat._23 = 0.0f; mat._24 = 0.0f; 
  164.         mat._31 = 0.0f; mat._32 = 0.0f; mat._33 = 1.0f; mat._34 = 0.0f; 
  165.         mat._41 = 0.5f; mat._42 = 0.5f; mat._43 = 0.0f; mat._44 = 1.0f; 
  166.         m_pd3dDevice->SetTransform( D3DTS_TEXTURE0, &mat );
  167.         m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT2 );
  168.         m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, D3DTSS_TCI_CAMERASPACENORMAL );
  169.  
  170.         // A glass effect can be achieved by turning on alpha-blending, and
  171.         // rendering both sides of the object
  172.         if( m_bGlassEffect )
  173.         {
  174.             m_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
  175.             m_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,  D3DBLEND_SRCCOLOR );
  176.             m_pd3dDevice->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_DESTCOLOR );
  177.             m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,  D3DCULL_NONE );
  178.             m_pd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE,     FALSE );
  179.         }
  180.  
  181.         // Finally, draw the teapot
  182.         m_pTeapot->UseMeshMaterials( FALSE );
  183.         m_pTeapot->Render( m_pd3dDevice );
  184.  
  185.         // Output statistics
  186.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  187.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  188.  
  189.         // End the scene.
  190.         m_pd3dDevice->EndScene();
  191.     }
  192.  
  193.     return S_OK;
  194. }
  195.  
  196.  
  197.  
  198.  
  199. //-----------------------------------------------------------------------------
  200. // Name: InitDeviceObjects()
  201. // Desc: Initialize scene objects.
  202. //-----------------------------------------------------------------------------
  203. HRESULT CMyD3DApplication::InitDeviceObjects()
  204. {
  205.     m_pFont->InitDeviceObjects( m_pd3dDevice );
  206.  
  207.     if( FAILED( m_pTeapot->Create( m_pd3dDevice, _T("Teapot.x") ) ) )
  208.         return D3DAPPERR_MEDIANOTFOUND;
  209.  
  210.     // Load the spheremap texture
  211.     if( FAILED( D3DUtil_CreateTexture( m_pd3dDevice, _T("Spheremap.bmp"), 
  212.                                        &m_pSphereMapTexture ) ) )
  213.         return D3DAPPERR_MEDIANOTFOUND;
  214.     
  215.     return S_OK;
  216. }
  217.  
  218.  
  219.  
  220.  
  221. //-----------------------------------------------------------------------------
  222. // Name: RestoreDeviceObjects()
  223. // Desc: Initialize scene objects.
  224. //-----------------------------------------------------------------------------
  225. HRESULT CMyD3DApplication::RestoreDeviceObjects()
  226. {
  227.     m_pFont->RestoreDeviceObjects();
  228.  
  229.     // Restore file objects
  230.     m_pTeapot->RestoreDeviceObjects( m_pd3dDevice );
  231.     
  232.     // Misc render states
  233.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  234.     m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  235.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
  236.     m_pd3dDevice->SetTextureStageState( 1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
  237.     m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE,   TRUE );
  238.     m_pd3dDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE );
  239.     m_pd3dDevice->SetRenderState( D3DRS_ZENABLE,        TRUE );
  240.     m_pd3dDevice->SetRenderState( D3DRS_AMBIENT,        0xffffffff );
  241.     m_pd3dDevice->SetRenderState( D3DRS_CULLMODE,       D3DCULL_NONE );
  242.  
  243.     // Set up world matrix
  244.     D3DXMATRIX matWorld;
  245.     D3DXMatrixIdentity( &matWorld );
  246.     m_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  247.  
  248.     // Set up view matrix
  249.     D3DXMATRIX  matView;
  250.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f, -4.5f );
  251.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 0.0f );
  252.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
  253.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  254.     m_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  255.  
  256.     // Set up proj matrix
  257.     D3DXMATRIX matProj;
  258.     FLOAT fAspect = ((FLOAT)m_d3dsdBackBuffer.Width) / m_d3dsdBackBuffer.Height;
  259.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 200.0f );
  260.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  261.  
  262.     // Setup a base material
  263.     D3DMATERIAL8 mtrl;
  264.     D3DUtil_InitMaterial( mtrl, 1.0f, 1.0f, 1.0f, 1.0f );
  265.     m_pd3dDevice->SetMaterial( &mtrl );
  266.  
  267.     // Set the ArcBall parameters
  268.     m_ArcBall.SetWindow( m_d3dsdBackBuffer.Width, m_d3dsdBackBuffer.Height, 1.0f );
  269.     m_ArcBall.SetRadius( 1.0f );
  270.  
  271.     return S_OK;
  272. }
  273.  
  274.  
  275.  
  276.  
  277. //-----------------------------------------------------------------------------
  278. // Name: InvalidateDeviceObjects()
  279. // Desc: 
  280. //-----------------------------------------------------------------------------
  281. HRESULT CMyD3DApplication::InvalidateDeviceObjects()
  282. {
  283.     m_pFont->InvalidateDeviceObjects();
  284.     m_pTeapot->InvalidateDeviceObjects();
  285.  
  286.     return S_OK;
  287. }
  288.  
  289.  
  290.  
  291.  
  292. //-----------------------------------------------------------------------------
  293. // Name: DeleteDeviceObjects()
  294. // Desc: Called when the app is exiting, or the device is being changed,
  295. //       this function deletes any device dependent objects.
  296. //-----------------------------------------------------------------------------
  297. HRESULT CMyD3DApplication::DeleteDeviceObjects()
  298. {
  299.     m_pFont->DeleteDeviceObjects();
  300.     SAFE_RELEASE( m_pSphereMapTexture );
  301.     m_pTeapot->Destroy();
  302.  
  303.     return S_OK;
  304. }
  305.  
  306.  
  307.  
  308.  
  309. //-----------------------------------------------------------------------------
  310. // Name: FinalCleanup()
  311. // Desc: Called before the app exits, this function gives the app the chance
  312. //       to cleanup after itself.
  313. //-----------------------------------------------------------------------------
  314. HRESULT CMyD3DApplication::FinalCleanup()
  315. {
  316.     SAFE_DELETE( m_pFont );
  317.     SAFE_DELETE( m_pTeapot );
  318.     SAFE_DELETE( m_pSphereMapTexture );
  319.  
  320.     return S_OK;
  321. }
  322.  
  323.  
  324.  
  325.  
  326. //-----------------------------------------------------------------------------
  327. // Name: MsgProc()
  328. // Desc: Message proc function to handle key and menu input
  329. //-----------------------------------------------------------------------------
  330. LRESULT CMyD3DApplication::MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam,
  331.                                     LPARAM lParam )
  332. {
  333.     // Pass mouse messages to the ArcBall so it can build internal matrices
  334.     m_ArcBall.HandleMouseMessages( hWnd, uMsg, wParam, lParam );
  335.  
  336.     // Trap context menu
  337.     if( WM_CONTEXTMENU == uMsg )
  338.         return 0;
  339.  
  340.     // Pass remaining messages to default handler
  341.     return CD3DApplication::MsgProc( hWnd, uMsg, wParam, lParam );
  342. }
  343.  
  344.  
  345.  
  346.  
  347.